﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Security;
using System.Threading;
using Moe.ECamel.Client.Controllers;
using SysWinForms = System.Windows.Forms;

using Moe.ECamel.Common.Contracts;
using Moe.ECamel.Common.Entities;

namespace Moe.ECamel.Client
{
	public static class ECamelHelper
	{
		private static SystemController _systemController;
		private static ECamelSystemInfo _systemInfo;

		public static bool Initialize()
		{
			SysWinForms.Application.ThreadException += Application_ThreadException;

			_systemController = new SystemController();
			_systemInfo = _systemController.LoadSystemInfo();
			return _systemInfo != null;
		}

		public static  SystemController SystemController
		{
			get { return _systemController; }
		}

		public static Dictionary<int, LibraryType> LibraryTypes
		{
			get { return _systemInfo.LibraryTypes; }
		}
		public static Dictionary<int, Role> Roles
		{
			get { return _systemInfo.Roles; }
		}
		public static Dictionary<int, Message> Messages
		{
			get { return _systemInfo.Messages; }
		}
		public static Dictionary<int, SubscriptionType> SubscriptionTypes
		{
			get { return _systemInfo.SubscriptionTypes; }
		}
		public static List<ItemType> ItemTypesList
		{
			get { return _systemInfo.ItemTypesList; }
		}

		private static DataView _itemPropertyTypes;
		public static DataView ItemPropertyTypes
		{
			get
			{
				if (_itemPropertyTypes == null)
				{
					DataTable dt = new DataTable();
					dt.Columns.Add("ItemPropertyTypeName", typeof(string));
					dt.Columns.Add("ItemPropertyTypeId", typeof(int));
					dt.Columns.Add("ItemTypeId", typeof(int));
					dt.Rows.Add("(אין)", 0, 0);
					foreach (ItemPropertyType ipt in _systemInfo.ItemPropertyTypes)
						dt.Rows.Add(ipt.ItemPropertyTypeName, ipt.ItemPropertyTypeId, ipt.ItemTypeId);
					_itemPropertyTypes = new DataView(dt);
				}

				return _itemPropertyTypes;
			}
		}

		public static SysWinForms.DialogResult ShowMessage(ECamelError error)
		{
			return ShowMessage(error.MessageId, error.Arguments);
		}
		public static SysWinForms.DialogResult ShowMessage(int messageId, params object[] args)
		{
			Message message = Messages[messageId];

			SysWinForms.MessageBoxIcon icon = GetIcon(message);
			SysWinForms.MessageBoxButtons buttons = GetButtons(message);
			string formattedText = (args == null ? message.Text : String.Format(message.Text, args));

			return SysWinForms.MessageBox.Show(formattedText, message.Caption, buttons, icon,
				SysWinForms.MessageBoxDefaultButton.Button1, SysWinForms.MessageBoxOptions.RightAlign | SysWinForms.MessageBoxOptions.RtlReading);
		}

		private static SysWinForms.MessageBoxIcon GetIcon(Message message)
		{
			switch (message.MessageTypeId)
			{
				case MessageTypes.Info:		return SysWinForms.MessageBoxIcon.Information;
				case MessageTypes.Warning:	return SysWinForms.MessageBoxIcon.Warning;
				case MessageTypes.Error:	return SysWinForms.MessageBoxIcon.Error;
				case MessageTypes.Question: return SysWinForms.MessageBoxIcon.Question;
				default:					return SysWinForms.MessageBoxIcon.Error;
			}
		}
		private static SysWinForms.MessageBoxButtons GetButtons(Message message)
		{
			switch (message.MessageTypeId)
			{
				case MessageTypes.Question: return SysWinForms.MessageBoxButtons.YesNo;
				default: return SysWinForms.MessageBoxButtons.OK;
			}
		}

		public static bool IsPermitted(String operation)
		{
			return Thread.CurrentPrincipal.IsInRole(operation);
		}

		private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
		{
			if (e.Exception is SecurityException)
				ShowMessage(MessageIds.SecurityInsufficientPermissions);
			else
				ShowMessage(MessageIds.UnexpectedError, e.Exception.Message);
		}
	}
}